<template>
  <div class="change-password-container">
    <div class="change-password-card">
      <div class="card-header">
        <h2><i class="fas fa-key"></i> 修改密码</h2>
        <p>为了账户安全，请定期修改密码</p>
      </div>

      <div class="card-body">
        <form @submit.prevent="submitForm" class="password-form">
          <div class="form-group">
            <label for="currentPassword">
              <i class="fas fa-lock"></i> 当前密码
            </label>
            <div class="input-wrapper">
              <input
                id="currentPassword"
                v-model="form.currentPassword"
                :type="showCurrentPassword ? 'text' : 'password'"
                class="form-control"
                placeholder="请输入当前密码"
                required
              />
              <button
                type="button"
                class="toggle-password"
                @click="showCurrentPassword = !showCurrentPassword"
              >
                <i :class="showCurrentPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
              </button>
            </div>
          </div>

          <div class="form-group">
            <label for="newPassword">
              <i class="fas fa-key"></i> 新密码
            </label>
            <div class="input-wrapper">
              <input
                id="newPassword"
                v-model="form.newPassword"
                :type="showNewPassword ? 'text' : 'password'"
                class="form-control"
                placeholder="请输入新密码（6-50位字符）"
                minlength="6"
                maxlength="50"
                required
              />
              <button
                type="button"
                class="toggle-password"
                @click="showNewPassword = !showNewPassword"
              >
                <i :class="showNewPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
              </button>
            </div>
            <div class="password-strength">
              <div class="strength-bar" :class="passwordStrength.level">
                <div class="strength-fill" :style="{ width: passwordStrength.percent + '%' }"></div>
              </div>
              <span class="strength-text">{{ passwordStrength.text }}</span>
            </div>
          </div>

          <div class="form-group">
            <label for="confirmPassword">
              <i class="fas fa-check-circle"></i> 确认新密码
            </label>
            <div class="input-wrapper">
              <input
                id="confirmPassword"
                v-model="form.confirmPassword"
                :type="showConfirmPassword ? 'text' : 'password'"
                class="form-control"
                :class="{ 'error': form.confirmPassword && form.newPassword !== form.confirmPassword }"
                placeholder="请再次输入新密码"
                required
              />
              <button
                type="button"
                class="toggle-password"
                @click="showConfirmPassword = !showConfirmPassword"
              >
                <i :class="showConfirmPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
              </button>
            </div>
            <div v-if="form.confirmPassword && form.newPassword !== form.confirmPassword" class="error-message">
              两次输入的密码不一致
            </div>
          </div>

          <div class="form-actions">
            <button
              type="button"
              class="btn btn-secondary"
              @click="$router.go(-1)"
            >
              <i class="fas fa-arrow-left"></i> 返回
            </button>
            <button
              type="submit"
              class="btn btn-primary"
              :disabled="!isFormValid || loading"
            >
              <i class="fas fa-spinner fa-spin" v-if="loading"></i>
              <i class="fas fa-save" v-else></i>
              {{ loading ? '修改中...' : '修改密码' }}
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import { useAuthStore } from '@/store/auth'

export default {
  name: 'ChangePassword',
  setup() {
    const authStore = useAuthStore()
    return { authStore }
  },
  data() {
    return {
      form: {
        currentPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      showCurrentPassword: false,
      showNewPassword: false,
      showConfirmPassword: false,
      loading: false
    }
  },
  computed: {
    isFormValid() {
      return (
        this.form.currentPassword &&
        this.form.newPassword &&
        this.form.confirmPassword &&
        this.form.newPassword === this.form.confirmPassword &&
        this.form.newPassword.length >= 6
      )
    },
    passwordStrength() {
      const password = this.form.newPassword
      if (!password) {
        return { level: '', percent: 0, text: '' }
      }

      let score = 0
      const checks = {
        length: password.length >= 8,
        lowercase: /[a-z]/.test(password),
        uppercase: /[A-Z]/.test(password),
        numbers: /\d/.test(password),
        symbols: /[^A-Za-z0-9]/.test(password)
      }

      score = Object.values(checks).filter(Boolean).length

      const levels = [
        { level: 'weak', percent: 20, text: '弱' },
        { level: 'fair', percent: 40, text: '一般' },
        { level: 'good', percent: 60, text: '良好' },
        { level: 'strong', percent: 80, text: '强' },
        { level: 'very-strong', percent: 100, text: '很强' }
      ]

      return levels[Math.min(score - 1, 4)] || levels[0]
    }
  },
  methods: {
    async submitForm() {
      if (!this.isFormValid) return

      this.loading = true
      try {
        await this.authStore.changePassword({
          currentPassword: this.form.currentPassword,
          newPassword: this.form.newPassword,
          confirmPassword: this.form.confirmPassword
        })

        this.$message.success('密码修改成功！')

        // 清空表单
        this.form = {
          currentPassword: '',
          newPassword: '',
          confirmPassword: ''
        }

        // 可选：跳转到其他页面
        setTimeout(() => {
          this.$router.push('/dashboard')
        }, 1500)

      } catch (error) {
        console.error('密码修改失败:', error)
        this.$message.error(error.response?.data?.message || '密码修改失败')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.change-password-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.change-password-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  width: 100%;
  max-width: 480px;
}

.card-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 2rem;
  text-align: center;
}

.card-header h2 {
  margin: 0 0 0.5rem 0;
  font-size: 1.8rem;
  font-weight: 600;
}

.card-header p {
  margin: 0;
  opacity: 0.9;
  font-size: 0.95rem;
}

.card-body {
  padding: 2rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  font-weight: 600;
  color: #374151;
  margin-bottom: 0.5rem;
  font-size: 0.95rem;
}

.form-group label i {
  margin-right: 0.5rem;
  color: #6366f1;
}

.input-wrapper {
  position: relative;
}

.form-control {
  width: 100%;
  padding: 0.75rem 2.5rem 0.75rem 0.75rem;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.form-control:focus {
  outline: none;
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.form-control.error {
  border-color: #ef4444;
}

.toggle-password {
  position: absolute;
  right: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #9ca3af;
  cursor: pointer;
  padding: 0.25rem;
}

.toggle-password:hover {
  color: #6366f1;
}

.password-strength {
  margin-top: 0.5rem;
}

.strength-bar {
  height: 4px;
  background: #e5e7eb;
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 0.25rem;
}

.strength-fill {
  height: 100%;
  transition: all 0.3s ease;
}

.strength-bar.weak .strength-fill { background: #ef4444; }
.strength-bar.fair .strength-fill { background: #f97316; }
.strength-bar.good .strength-fill { background: #eab308; }
.strength-bar.strong .strength-fill { background: #22c55e; }
.strength-bar.very-strong .strength-fill { background: #16a34a; }

.strength-text {
  font-size: 0.85rem;
  color: #6b7280;
}

.error-message {
  color: #ef4444;
  font-size: 0.85rem;
  margin-top: 0.25rem;
}

.form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 2rem;
}

.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  background: #f3f4f6;
  color: #374151;
}

.btn-secondary:hover:not(:disabled) {
  background: #e5e7eb;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  flex: 1;
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

@media (max-width: 480px) {
  .change-password-container {
    padding: 10px;
  }

  .card-header,
  .card-body {
    padding: 1.5rem;
  }

  .form-actions {
    flex-direction: column;
  }
}
</style>